<?php
namespace app\admin\controller;
use app\admin\controller\Common;
use think\Db;
use app\admin\model\PayType as PayTypeMx;

class Goldcoin extends Common{
    
    public function lst(){
        $list = Db::name('gold_coin_recharge')->select();
        $liveConfig = db('live_config')->where(['id'=>1])->find();
        foreach ($list as $key=>$value){
            $list[$key]['gold_coin'] = $value['money'] * $liveConfig['gold_coin_to_rmb'];
        }

        $this->assign('list',$list);// 赋值数据集
        return $this->fetch();
    }


    public function edit(){
        if(request()->isAjax()){
            if(input('post.id')){
                $data = input('post.');
                if(!preg_match('/^[1-9]+\d*(.\d{1,2})?$|^\d+.\d{1,2}$/',$data['money'])){
                    $value = array('status'=>0,'mess'=>"充值金额错误");
                }else{
                    $gold_coin_recharge = Db::name('gold_coin_recharge')->where('id',$data['id'])->find();
                    if($gold_coin_recharge){
                        $gold_coin_save = Db::name('gold_coin_recharge')->update(['id'=>$data['id'],'money'=>$data['money'],'updated'=>time()]);
                        if($gold_coin_save !== false){
                            ys_admin_logs('编辑支付方式','gold_coin_recharge',$data['id']);
                            $value = array('status'=>1,'mess'=>'编辑成功');
                        }else{
                            $value = array('status'=>0,'mess'=>'编辑失败');
                        }
                    }else{
                        $value = array('status'=>0,'mess'=>'找不到相关信息');
                    }
                }
            }else{
                $value = array('status'=>0,'mess'=>'缺少参数，编辑失败');
            }
            return json($value);
        }else{
            if(input('id')){
                $find = Db::name('gold_coin_recharge')->find(input('id'));
                if($find){
                    $this->assign('find', $find);
                    return $this->fetch();
                }else{
                    $this->error('找不到相关信息');
                }
            }else{
                $this->error('缺少参数');
            }
        }
    }
}
